﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz;
using Quartz.Impl;
using iemtm.WeChat.Core.Quartz.Job;

namespace iemtm.WeChat.Core.Quartz
{
    public class QuartzStartUp
    {
        //调度工厂
        private ISchedulerFactory _factory;
        //调度器
        private IScheduler _scheduler;


        /// <summary>
        /// 初始化参数
        /// </summary>
      


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public async Task<string> Start()
        {
            

 


            //1、声明一个调度工厂
            _factory = new StdSchedulerFactory();
            //2、通过调度工厂获得调度器
            _scheduler = await _factory.GetScheduler();
            //3、开启调度器
            await _scheduler.Start();

            //4、创建任务
       
            IJobDetail checkWeChatAccessTokenJob = JobBuilder.Create<CheckWeChatAccessTokenJob>().Build();
            IJobDetail WxPushAlarmJob = JobBuilder.Create<WxPushAlarm>().Build();
            IJobDetail WxPushInformationJob = JobBuilder.Create<WxPushInformation>().Build();
            IJobDetail WxPushTowerAnnounceAuditJob = JobBuilder.Create<WxPushTowerAnnounceAudit>().Build();
            IJobDetail WxPushOperatorAnnounceAuditJob = JobBuilder.Create<WxPushOperatorAnnounceAudit>().Build();
            IJobDetail CheckGeneratorRunTimeJob = JobBuilder.Create<CheckGeneratorRunTime>().Build();



            //5、创建一个触发器

            ITrigger triggerCheckWeChatAccessTokenJob = TriggerBuilder.Create()
                .WithIdentity("CheckWeChatAccessTokenJob", "CheckWeChatAccessToken")
                .WithSimpleSchedule(t => t.WithIntervalInSeconds(5).RepeatForever())
                .StartNow()
                .Build(); //5秒钟执行一次
            ITrigger WxPushAlarmTrigger = TriggerBuilder.Create()
               .WithIdentity("WxPushAlarmJob", "WxPushAlarmTrigger")
               .StartNow()
               .WithSimpleSchedule(t => t.WithIntervalInSeconds(5).RepeatForever())
               .Build(); //5秒钟执行一次
            ITrigger WxPushInformationTrigger = TriggerBuilder.Create()
               .WithIdentity("WxPushInformationJob", "WxPushInformationTrigger")
               .StartNow()
               .WithSimpleSchedule(t => t.WithIntervalInSeconds(5).RepeatForever())
               .Build(); //5秒钟执行一次
            ITrigger WxPushTowerAnnounceAuditTrigger = TriggerBuilder.Create()
               .WithIdentity("WxPushTowerAnnounceAuditJob", "WxPushTowerAnnounceAuditTrigger")
               .StartNow()
               .WithCronSchedule("0 30 8 * * ?")//每天08:30运行
               .Build();
            ITrigger WxPushOperatorAnnounceAuditTrigger = TriggerBuilder.Create()
               .WithIdentity("WxPushOperatorAnnounceAuditJob", "WxPushOperatorAnnounceAuditTrigger")
               .StartNow()
               .WithCronSchedule("0 30 8 * * ?")//每天08:30运行
               .Build();
            ITrigger CheckGeneratorRunTimeTrigger = TriggerBuilder.Create()
              .WithIdentity("CheckGeneratorRunTimeJob", "CheckGeneratorRunTimeTrigger")
              .StartNow()
              .WithCronSchedule("0 0 15 ? * SUN")//每周日15点运行
              .Build();



            //6、将触发器和任务器绑定到调度器中
            await _scheduler.ScheduleJob(checkWeChatAccessTokenJob, triggerCheckWeChatAccessTokenJob);
            await _scheduler.ScheduleJob(WxPushAlarmJob, WxPushAlarmTrigger);
            await _scheduler.ScheduleJob(WxPushInformationJob, WxPushInformationTrigger);
            await _scheduler.ScheduleJob(WxPushTowerAnnounceAuditJob, WxPushTowerAnnounceAuditTrigger);
            await _scheduler.ScheduleJob(WxPushOperatorAnnounceAuditJob, WxPushOperatorAnnounceAuditTrigger);
            await _scheduler.ScheduleJob(CheckGeneratorRunTimeJob, CheckGeneratorRunTimeTrigger);



            return await Task.FromResult("将触发器和任务器绑定到调度器中完成");
        }


        public void Stop()
        {
            if (_scheduler != null)
            {
                _scheduler.Clear();
                _scheduler.Shutdown();
            }
            _scheduler = null;
            _factory = null;
        }
    }
}

